<template>
  <div>
    <el-form ref="myform" :model="formItem" :rules="rules" label-width="100px" size="mini">
      <el-form-item label="小组名称" prop="name">
        <el-input v-model="formItem.name" placeholder="请输入小组名称" />
      </el-form-item>
      <el-form-item label="小组组长" prop="groupLeaderCode">
        <el-select v-model="formItem.groupLeaderCode" filterable clearable placeholder="请选择小组组长">
          <el-option v-for="item in availableUsers" :key="item.username" :label="item.name" :value="item.username" />
        </el-select>
      </el-form-item>
      <el-form-item label="单位" prop="institutions">
        <el-select v-model="formItem.institutions" filterable clearable multiple placeholder="请选择单位">
          <el-option v-for="item in availableInstitutions" :key="item.code" :label="item.name" :value="item.code" />
        </el-select>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
const formRules = {
  name: [{ required: true, message: '小组名称不能为空', trigger: 'blur' }],
  groupLeaderCode: [{ required: true, message: '小组组长不能为空', trigger: 'blur' }],
  institutions: [{ required: true, message: '单位不能为空', trigger: 'blur' }]
}

import { create, update, detail } from '@neu/dcom-ui/api/operationGroup'
import { list as userList } from '@neu/auth-ui/api/user'
import { list as institutionsList } from '@neu/dcom-ui/api/institutions'
// 定义一个表单原始项
const formItem = {
  // 运维组名称
  name: '',
  // 运维组编码
  code: '',
  // 组长名称
  groupLeaderName: '',
  // 组长编码
  groupLeaderCode: null,
  // 关联单位编码
  institutions: []
}
export default {
  name: 'OperationGroupEditForm',
  props: {
    // 根据id可判断是新建还是更新
    id: {
      required: false,
      type: Number,
      default: -1
    }
  },
  data() {
    return {
      // 编辑的表单内容
      formItem: {
        ...formItem
      },
      // 所有人员
      availableUsers: [],
      // 所有单位
      availableInstitutions: [],
      // 表单验证规则
      rules: { ...formRules }
    }
  },
  watch: {
    // 监控id的变化，初始化表单数据
    id: function() {
      this.fetch()
    }
  },
  created() {
    this.fetch()
    userList({ 's_roleIn': 'workerGroupLeader,bigdataBureau' }).then(res => {
      this.availableUsers = res.data.map(e => { return { name: e.name, username: e.username } })
    })
    institutionsList().then(res => {
      this.availableInstitutions = res.data.map(e => { return { name: e.name, code: e.code } })
    })
  },
  methods: {
    // 初始化表单数据
    fetch() {
      if (this.id !== -1) {
        detail(this.id).then(response => {
          const { id, name, code, groupLeaderName, groupLeaderCode, institutionsList } = response.data
          const institutions = (institutionsList || []).map(e => e.code)
          this.formItem = { id, name, code, groupLeaderName, groupLeaderCode, institutions }
        })
      } else {
        this.formItem = {
          ...formItem
        }
      }
    },
    // 保存操作，返回promise，后续处理交由调用者处理
    save() {
      const updated = this.id !== -1
      return new Promise(resolve => {
        this.$refs.myform.validate((valid) => {
          if (valid) {
            const { name, code, groupLeaderCode, institutions } = this.formItem
            // 组长名称
            const filterData = this.availableUsers.find(e => e.username === groupLeaderCode)
            const groupLeaderName = filterData ? filterData.name : ''
            const saveData = { name, code, groupLeaderCode, groupLeaderName, institutions }
            if (updated) {
              resolve(update(this.id, saveData))
            } else {
              resolve(create(saveData))
            }
          } else {
            resolve(false)
          }
        })
      })
    },
    reset() {
      this.$refs.myform.resetFields()
    }
  }
}
</script>
